<?php
class links_edit
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function links_edit($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		if (! $this->Links->edit_perm) {
			$this->Links->linksRedirectHeader(XOOPS_URL . '/user.php', 2, _NOPERM);
			exit();
		}
	}

	function index($lid = NULL)
	{
		$this->form($lid);
	}

	function form($lid = NULL)
	{
		if (!empty($lid)) {

			if(! $link = $this->Links->getDataByLid($lid)){
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
				exit();
			}

			if (!$this->Links->edit_perm || !($this->Links->isadmin || $link['submitter'] == $this->Links->uid) ){
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _NOPERM);
				exit();
			}

			$cid_arr = $link['cid'];
			if ($this->Links->category_selector || $this->Links->is_mobile) {//multiple
				$selbox = $this->Links->Categories->makeMySelBox('corder,cid', $cid_arr, FALSE, 'cid[]', '', 'cid', 1);
			} else {//singles
				$selbox = array();
				for( $i=0; $i<count($cid_arr); $i++ ){
					$selbox[$i] = $this->Links->Categories->makeMySelBox('corder,cid', $cid_arr[$i], TRUE, 'cid[]', '', 'cid', 0);
				}
			}

			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => $link['title']);

			$data = array(
				'link'              => $link,
				'xoops_breadcrumbs' => $bread,
				'category_selbox'   => $selbox ,
				'date_selector'     => $this->Links->makeDateSelector($link['date']),
				'gticket'           => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_edit'),
				'gticket_del'       => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_delete'),
			);

			$template = $this->Links->getTemplatePrefix() . '_edit.html';
			$this->Links->view($template, $data);

		} else {
			$this->links_redirect_header($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function post()
	{
		$js_check = $this->Links->is_mobile ? FALSE: TRUE;
		$this->Links->basicPostCheck('links_edit', $js_check);

		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		$link = $this->Links->getDataByLid($lid);

		if (!$this->Links->edit_perm || !($this->Links->isadmin || $link['submitter'] == $this->Links->uid) ){
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _NOPERM);
			exit();
		}

		if ($this->Links->update($lid, $link['status'])) {// TODO
			if ($this->Links->edit_approval == TRUE) {
				$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid'=>$lid)), 2, _MD_MODIFY_OK);
				exit;
			} else {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_MODIFY_OK_NOTAPP);
				exit;
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid'=>$lid)), 5, 'DB ERROR'); //TODO
			exit;
		}

	}

	// MOBILE only
	function delete_confirm()
	{
		if (!$this->Links->del_perm || !$this->Links->is_mobile) {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _NOPERM);
			exit();
		}

		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		if ($link = $this->Links->getDataByLid($lid)) {
			$template = $this->Links->getTemplatePrefix() . '_delete_confirm.html';
			$data = array(
				'link'        => $link,
				'gticket_del' => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__,1800,'links_delete'),
			);
			$this->Links->view($template, $data);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function delete()
	{
		if ($this->Links->del_perm && isset($_POST['dellink']) && $_POST['dellink'] == 1) {

			$this->Links->basicPostCheck('links_delete', FALSE);

			if (!isset($_POST['lid']) || empty($_POST['lid'])) {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
				exit();
			}
			$lid = intval($_POST['lid']) ;

			if ($this->Links->delete($lid)) {
				if (!$this->Links->del_approval) {
					$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_LINK_DEL_REQ);
				}else{
					$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_LINKDELETED);
				}
				exit();
			} else {
				$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid'=>$lid)), 5, 'DB ERROR'); //TODO
				exit();
			}

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _NOPERM);
			exit();
		}
	}



}
